package com.jones.restaurant.ui.activitys;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.jones.restaurant.R;
import com.jones.restaurant.R.layout;
import com.jones.restaurant.R.menu;
import com.jones.restaurant.beans.Menu;
import com.jones.restaurant.beans.MenuBeans;
import com.jones.restaurant.framework.AppConfig;
import com.jones.restaurant.framework.BaseActivity;
import com.jones.restaurant.utils.DensityUtil;
import com.jones.restaurant.utils.SortBeanUtils;
import com.jones.restaurant.utils.StringUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.location.Location;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class HomeActivity extends BaseActivity {

	private String REQUEST_MENU_LIST = "menu.php";
	private ViewPager vp_main_page;
	private ArrayList<View> views;
	private LinearLayout menu_layout_of_pager;
	private ListView lv_left_menu_category;
	private ListView lv_right_menus;
	private RequestQueue requestqueue;
	private RelativeLayout include_loading_hint;
	private List<Menu> leftList;
	private List<Menu> rightList;
	private LeftMenuCategoryAdapter leftAdapter;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_home);
		
		requestqueue = Volley.newRequestQueue(this);
		initView();
		initData();
		initListener();
	}

	private void initData() {
		get(REQUEST_MENU_LIST, null);
		PagerAdapter pageAdapter = new PagerAdapter() {
			
			@Override
			public boolean isViewFromObject(View arg0, Object arg1) {
				// TODO Auto-generated method stub
				return arg0 == arg1;
			}
			
			@Override
			public int getCount() {
				// TODO Auto-generated method stub
				return views.size();
			}

			@Override
			public void destroyItem(View container, int position, Object object) {
				((ViewPager)container).removeView(views.get(position));
			}

			@Override
			public Object instantiateItem(View container, int position) {
				((ViewPager)container).addView(views.get(position));
				return views.get(position);
			}
			
			
		};
		vp_main_page.setAdapter(pageAdapter);
		
	}

	public void initView() {
		vp_main_page = (ViewPager) findViewById(R.id.vp_main_page);
		
		
		menu_layout_of_pager = (LinearLayout) getLayoutInflater().inflate(R.layout.menu_layout_of_pager, null);
		lv_left_menu_category = (ListView) menu_layout_of_pager.findViewById(R.id.lv_left_menu_category);
		lv_right_menus = (ListView) menu_layout_of_pager.findViewById(R.id.lv_right_menus);
		include_loading_hint = (RelativeLayout)findViewById(R.id.include_loading_hint);
		
		
		views = new ArrayList<View>();
		TextView tv2 = new TextView(getApplicationContext());
		tv2.setText("第二个页面");
		TextView tv3 = new TextView(getApplicationContext());
		tv3.setText("第三个页面");
		views.add(menu_layout_of_pager);
		views.add(tv2);
		views.add(tv3);
	}

	public void initListener() {
		vp_main_page.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				switch(arg0){
				case 0:
//					Toast.makeText(getApplicationContext(), "0", 0).show();
					break;
				case 1:
//					Toast.makeText(getApplicationContext(), "1", 0).show();
					break;
				case 2:
//					Toast.makeText(getApplicationContext(), "2", 0).show();
					break;
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
				// TODO Auto-generated method stub
				
			}
		});
		
		lv_left_menu_category.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				lv_right_menus.setSelection(leftList.get(position).getLeft2right());
				leftAdapter.setSelectFlag(position);
			}
		});
		lv_right_menus.setOnScrollListener(new OnScrollListener() {
			
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
			}
			
			@Override
			public void onScroll(AbsListView view, int firstVisibleItem,
					int visibleItemCount, int totalItemCount) {
				if(lv_left_menu_category!=null&&rightList!=null){
					leftAdapter.setSelectFlag(rightList.get(firstVisibleItem).getRight2left());
				}
			}
		});
	}

	public class LeftMenuCategoryAdapter extends BaseAdapter{
		int a = -1;  
        public void setSelectFlag(int flag){  
            if (flag==a) {  
                return;  
            }  
            a = flag;  
            notifyDataSetChanged();  
        }  
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return leftList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@SuppressLint("ResourceAsColor")
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView tv = new TextView(getApplicationContext());
			tv.setBackgroundColor(getResources().getColor(R.color.menu_left_bg));
			tv.setTextColor(getResources().getColor(R.color.black));
			tv.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT, DensityUtil.dip2px(getApplicationContext(), 50)));
			tv.setGravity(Gravity.CENTER);
			tv.setText(leftList.get(position).getCourse_category_name());
			 if(a != -1&& position == a){  
				 tv.setSelected(true);  
				 tv.setTextColor(getResources().getColor(R.color.menu_left_selected_text));
				 tv.setBackgroundColor(getResources().getColor(R.color.white));
             } 
			return tv;
		}
		
	}
	
	public class RightMenuAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return rightList.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			if(rightList.get(position).getCourse_name() == null){
				TextView tv1 = new TextView(getApplicationContext());
				tv1.setLayoutParams(new ListView.LayoutParams(LayoutParams.FILL_PARENT,DensityUtil.dip2px(getApplicationContext(), 30)));
				tv1.setGravity(Gravity.CENTER_VERTICAL);
				tv1.setBackgroundColor(getResources().getColor(R.color.menu_right_category_title_bg));
				tv1.setTextColor(getResources().getColor(R.color.black));
				tv1.setText(rightList.get(position).getCourse_category_name());
				return tv1;
			}else{
				RightViewHolder rightViewHolder = null;
				if(convertView != null && convertView instanceof RelativeLayout){
					rightViewHolder = (RightViewHolder) convertView.getTag();
				}else{
					rightViewHolder = new RightViewHolder();
					convertView =  getLayoutInflater().inflate(R.layout.item_right_menu_layout, null);
					rightViewHolder.tv_menu_name = (TextView) convertView.findViewById(R.id.tv_menu_name);
					rightViewHolder.tv_on_sale_month = (TextView) convertView.findViewById(R.id.tv_on_sale_month);
					rightViewHolder.tv_menu_price = (TextView) convertView.findViewById(R.id.tv_menu_price);
					rightViewHolder.tv_menu_comment = (TextView) convertView.findViewById(R.id.tv_menu_comment);
					rightViewHolder.tv_menu_sum = (TextView) convertView.findViewById(R.id.tv_menu_sum);
					rightViewHolder.iv_menu_thumb = (ImageView) convertView.findViewById(R.id.iv_menu_thumb);
					rightViewHolder.iv_case_plus = (ImageView) convertView.findViewById(R.id.iv_case_plus);
					rightViewHolder.iv_case_minus = (ImageView) convertView.findViewById(R.id.iv_case_minus);
					convertView.setTag(rightViewHolder);
				}
				rightViewHolder.tv_menu_name.setText(rightList.get(position).getCourse_name());
				rightViewHolder.tv_on_sale_month.setText(rightList.get(position).getCourse_name());
				rightViewHolder.tv_menu_price.setText(rightList.get(position).getCourse_price()+"元/份");
				if(StringUtils.isBlank(rightList.get(position).getCourse_describe())){
					rightViewHolder.tv_menu_comment.setVisibility(View.GONE);
				}else{
					rightViewHolder.tv_menu_comment.setVisibility(View.VISIBLE);
					rightViewHolder.tv_menu_comment.setText(rightList.get(position).getCourse_describe());
				}
				if(StringUtils.isBlank(rightList.get(position).getCourse_thumb_url())){
					rightViewHolder.iv_menu_thumb.setVisibility(View.GONE);
				}else{
					rightViewHolder.iv_menu_thumb.setVisibility(View.VISIBLE);
					ImageLoader.getInstance().displayImage(AppConfig.rootDomain+rightList.get(position).getCourse_thumb_url(), rightViewHolder.iv_menu_thumb); 
				}
				if(rightList.get(position).getSum()>0){
					rightViewHolder.iv_case_plus.setImageResource(R.drawable.takeout_bg_btn_increase_focus);
					rightViewHolder.iv_case_minus.setVisibility(View.VISIBLE);
					rightViewHolder.tv_menu_sum.setVisibility(View.VISIBLE);
				}else{
					rightViewHolder.iv_case_plus.setImageResource(R.drawable.takeout_bg_btn_increase);
					rightViewHolder.iv_case_minus.setVisibility(View.GONE);
					rightViewHolder.tv_menu_sum.setVisibility(View.GONE);
				}
				rightViewHolder.tv_menu_sum.setText(rightList.get(position).getSum()+"");
				final TextView tempTextView = rightViewHolder.tv_menu_sum;
				final ImageView tempPlus = rightViewHolder.iv_case_plus;
				final ImageView iv_case_minus = rightViewHolder.iv_case_minus;
				rightViewHolder.iv_case_plus.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						int sum = Integer.parseInt((String) tempTextView.getText());
						if(sum==0){
							tempPlus.setImageResource(R.drawable.takeout_bg_btn_increase_focus);
						}
						sum++;
						tempTextView.setText(sum+"");
						rightList.get(position).setSum(sum);
						tempTextView.setVisibility(View.VISIBLE);
						iv_case_minus.setVisibility(View.VISIBLE);
					}
				});
				rightViewHolder.iv_case_minus.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						int sum = Integer.parseInt((String) tempTextView.getText());
						if(sum==1){
							tempPlus.setImageResource(R.drawable.takeout_bg_btn_increase);
							tempTextView.setVisibility(View.GONE);
							iv_case_minus.setVisibility(View.GONE);
						}
						if(sum>-1){
							sum--;
							rightList.get(position).setSum(sum);
							tempTextView.setText(sum+"");
						}
					}
				});
				return convertView;
			}
		}
		
	}
	
	class RightViewHolder{
		TextView tv_menu_name;
		TextView tv_on_sale_month;
		TextView tv_menu_price;
		TextView tv_menu_comment;
		TextView tv_menu_sum;
		ImageView iv_menu_thumb;
		ImageView iv_case_plus;
		ImageView iv_case_minus;
	}
	
	@Override
	public void onReceive(String fullUrl, String suffixUrl, String response) {
		// TODO Auto-generated method stub
		super.onReceive(fullUrl, suffixUrl, response);
		
		if(suffixUrl.equals(REQUEST_MENU_LIST)){
			MenuBeans menuBeans = JSONObject.parseObject(response,MenuBeans.class);
			if(menuBeans.getCode()==0){
				include_loading_hint.setVisibility(View.GONE);
				for(int i = 0;i<menuBeans.getData().size();i++){
					SortBeanUtils.addMenu(menuBeans.getData().get(i));
				}
				leftList = SortBeanUtils.getLeftOrderlyList();
				rightList = SortBeanUtils.getOrderlyList(); 
				leftAdapter = new LeftMenuCategoryAdapter();
				lv_left_menu_category.setAdapter(leftAdapter);
				RightMenuAdapter rightMenuAdapter = new RightMenuAdapter();
				lv_right_menus.setAdapter(rightMenuAdapter);
			}
		}
	}
}
